package com.zmn.oms.third.jddj.service.impl;

import com.zmn.common.utils.number.NumberUtil;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.constant.OrderLogConsts;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.entity.order.OrderExtend;
import com.zmn.oms.model.entity.work.OrderWorkLogChange;
import com.zmn.oms.services.interfaces.order.OrderExtendService;
import com.zmn.oms.third.jddj.JddjConsts;
import com.zmn.oms.third.jddj.service.JddjService;
import com.zmn.oms.zmn.business.interfaces.open.OrderChangeNotifyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 京东到家渠道对接订单状态处理实现
 *
 * @author xiewenbing
 * @date  2020/01/18 17:00
 */
@Slf4j
@Service("jddjNotifyService")
public class JddjNotifyServiceImpl implements OrderChangeNotifyService {

    @Autowired
    private JddjService jddjService;

    @Autowired
    private OrderExtendService orderExtendService;

    @Override
    public void notifyChange(OrderWorkLogChange orderWorkLogChange) {

        // 查询该订单是否在CDS中存在，开始
        OrderExtend dbOrderExtend = orderExtendService.findMiniOrderExtendByOrderId(orderWorkLogChange.getWorkId());
        if (NumberUtil.isNotNullOrZero(dbOrderExtend.getClueId())) {
            log.info(
                    "【京东到家】CDS录入订单，由CDS负责接单，orderWorkLogChange=[{}]",
                    com.github.ltsopensource.core.json.JSON.toJSONString(orderWorkLogChange)
            );
            return;
        }

        // 查询该订单是否在CDS中存在，结束

        // 判断功能是否开启
        if (!JddjConsts.POWER_ON) {
            log.info("【{}】<订单状态同步> 该功能没有开启。", JddjConsts.CHANNEL_NAME);
            return;
        }

        if (!Objects.equals(orderWorkLogChange.getInputType(),OrderConsts.ORDER_INPUT_TYPE_API)) {
            log.info("【{}】的录入方式不为API接入，不需要状态同步", JddjConsts.CHANNEL_NAME);
            return;
        }

        log.info("【{}】<订单状态同步> ,状态信息:[{}]。", JddjConsts.CHANNEL_NAME,orderWorkLogChange.toString());

        int logType = orderWorkLogChange.getLogType().intValue();

        try {
            switch (logType) {

                // 确认
                case OrderLogConsts.ORDER_LOG_TYPE_CONFIRM:
                    jddjService.syncOrderConfirm(orderWorkLogChange);
                    break;

                /**
                // 上门
                case OrderLogConsts.ORDER_LOG_TYPE_VISIT:
                    jddjService.syncMasterVisit(orderWorkLogChange);
                    break;
                **/

                // 服务完成
                case OrderLogConsts.ORDER_LOG_TYPE_COMPLETE:
                    jddjService.syncOrderComplete(orderWorkLogChange);
                    break;

                // 取消、失败、弃单 - 同步取消
                case OrderLogConsts.ORDER_LOG_TYPE_CANCEL:
                case OrderConsts.ORDER_OP_TYPE_FAIL:
                case OrderConsts.ORDER_OP_TYPE_DISCARD:
                    jddjService.syncOrderCancel(orderWorkLogChange);
                    break;

                default:
                    log.info("【{}】<订单状态-【{}】不同步> ", JddjConsts.CHANNEL_NAME,OrderLogConsts.getOrderLogTypeName(logType));
                    return;
            }
        } catch (OmsBaseException exception) {

            log.info("【{}】<订单状态-【{}】同步> 失败 【{}】 ", JddjConsts.CHANNEL_NAME,OrderLogConsts.getOrderLogTypeName(logType),
                    exception.getMessage());
        }

        log.info("【{}】<订单状态-【{}】同步> 完成 ", JddjConsts.CHANNEL_NAME,OrderLogConsts.getOrderLogTypeName(logType));
    }
}
